package com.ht.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.ht.domain.bo.ArticleBO;
import com.ht.domain.po.*;
import com.ht.domain.vo.ArticleVO;
import com.ht.mapper.*;
import com.ht.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章service
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private HospitalMapper hospitalMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PopularizationMapper popularizationMapper;

    @Autowired
    private LevelMapper levelMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 根据知识id查询相关科普文章
     * @param knowledgeId
     * @return
     */
    @Override
    public List<ArticleBO> findArticleListByknowledgeId(Long knowledgeId) {
        Criteria criteria = Criteria.where("knowledge_id").is(knowledgeId.toString());
        criteria.and("article_status").is("1");
        List<Article> articleList = mongoTemplate.find(new Query(criteria),Article.class);
        List<ArticleBO> articleBOList = new ArrayList<>();
        for (Article article:articleList) {
            ArticleBO articleBO = new ArticleBO();
            TbHospital hospital = hospitalMapper.selectById(Long.valueOf(article.getHospital()));
            TbDepartment department = departmentMapper.selectById(Long.valueOf(article.getDepartment()));
            TbPopularization popularization = popularizationMapper.selectById(Long.valueOf(article.getPopularizationId()));
            TbLevel level = levelMapper.selectById(Long.valueOf(article.getLevel()));
            TbDoctor doctor = doctorMapper.selectById(Long.valueOf(article.getDoctor()));
            BeanUtils.copyProperties(article,articleBO);
            //医院名称
            articleBO.setHospitalName(hospital.getHospitalName());
            //科室名称
            articleBO.setDepartmentName(department.getDepartmentName());
            //科普号名称
            articleBO.setPopularizationName(popularization.getPopularizationName());
            //医生名称
            articleBO.setDoctorName(doctor.getDoctorName());
            //职位名称
            articleBO.setLevelName(level.getLevel());
            articleBOList.add(articleBO);
        }
        return articleBOList;
    }
}
